﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol.officer;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Tools;

namespace Pixysoft.Weblications.Sango.Command.officer
{
    partial class SangoOfficerHandler
    {
        public officer_upgrade officer_upgrade(string primarycode)
        {
            string userCode = SangoManager.Authentication();

            officer_upgrade response = ReflectionManager.CreatePojo<officer_upgrade>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);

            long timeMode = UpgradeTimeTypecodeCollection.MODE_2;


            // 达到最大等级

            Entities.Usr_Officer officer = OfficerDao.getUserOfficer(userCode, primarycode);

            if (OfficerDao.isUpgrading(officer))
            {
                response.Message = "武将正在训练";
                return response;
            }

            if (officer.Officerlevel >= profile.Userlevel)
            {
                response.Message = "武将等级无法超越总部等级";
                return response;
            }

            // 正在训练

            Entities.Usr_Building officerBuilding = UsrBuildingDao.getUserBuilding(userCode, CtiBuildingTypecodeCollection.TRAIN);
            long soulvalue = OfficerFormular.getUpgradeTimeModeCostSoulPrice(officerBuilding.Buildinglevel, timeMode);
            long startdate = GlobalTimer.Instance.ToTimeSequence();
            long timespan = OfficerFormular.getUpgradeTimeModeTimeSpan(timeMode);

            Entities.Usr_Queue queue = UsrQueueDao.UsrQueueStartup(userCode, CtiQueueTypecodeCollection.OFFICER_UPGRADE, officer.Primarycode, timespan);
            if (queue == null)
            {
                response.Message = "无训练位置";
                return response;
            }

            if (!UsrProfileDao.tryDecreaseSoulValue(profile, soulvalue))
            {
                response.Message = "军魂不足";
                return response;
            }

            int speedMode = UpgradeSpeedTypecodeCollection.MODE_1;
            if (timeMode == UpgradeTimeTypecodeCollection.MODE_1)
            {
                speedMode = UpgradeSpeedTypecodeCollection.MODE_2;
            }
            OfficerDao.startUpgrade(officer, startdate, timespan, speedMode, timeMode);

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                // command.begin();

                command.Update(queue);

                command.Update(officer);

                command.Update(profile);

                command.Commit();
            });

            response.profile = UsrProfileFacade.ToProfile(profile);
            response.officer = OfficerFacade.ToOfficer(officer);
            response.queue = UsrQueueFacade.ToQueue(queue);
            response.Succeed = true;
            return response;
        }


    }
    public interface officer_upgrade : ISangoResponse
    {
        player_profile profile { get;set;}
        player_queue queue { get;set;}
        player_officer officer { get;set;}
    }
}
